package org.autumn.spring;

import lombok.Data;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

@Data
public class UserService {
    PlatformTransactionManager transactionManager;
    JdbcTemplate jdbcTemplate;


    public void updateUserMoney(Integer id, Integer money) {
        TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            String sql = "UPDATE `mk_base`.`user` SET `money` = " + money + " WHERE `id` = " + id + "";
            jdbcTemplate.execute(sql);
            int i = 1 / 0;
            transactionManager.commit(transaction);
        } catch (Exception e) {
            transactionManager.rollback(transaction);
            e.printStackTrace();
        }
    }

    public void updateUserMoneyNoInvade(Integer id, Integer money) {
        String sql = "UPDATE `mk_base`.`user` SET `money` = " + money + " WHERE `id` = " + id + "";
        jdbcTemplate.execute(sql);
        int i = 1 / 0;

    }

}
